package zengjin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import zengjin.mapper.*;
import zengjin.model.dto.admin.UpdateAdminPersonalDto;
import zengjin.model.en.*;
import zengjin.model.vo.admin.AdminDateVO;
import zengjin.model.vo.admin.AdminEducationDataVO;
import zengjin.model.vo.admin.AdminEmployerDataVO;
import zengjin.model.vo.admin.AdminJobDataVO;
import zengjin.service.AdminService;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author 19324
 * @description 针对表【admin】的数据库操作Service实现
 * @createDate 2025-01-02 14:22:19
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
        implements AdminService {

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private ImageMapper imageMapper;

    @Resource
    private JobMapper jobMapper;

    @Resource
    private EmployerMapper employerMapper;

    @Resource
    private JobseekerMapper jobseekerMapper;

    @Override
    public boolean updateAdminPersonal(UpdateAdminPersonalDto adminPersonalDto) {
        //1.获取参数
        Long adminId = adminPersonalDto.getAdminId();
        String adminUsername = adminPersonalDto.getAdminUsername();
        String adminPhone = adminPersonalDto.getAdminPhone();
        String userAvatarUrl = adminPersonalDto.getUserAvatarUrl();

        //2.更新头像信息
        int update = 0;
        if (StrUtil.isNotBlank(userAvatarUrl)) {
            LambdaUpdateWrapper<Image> updateImgWrapper = new LambdaUpdateWrapper<>();
            updateImgWrapper.eq(Image::getImageNameId, adminId)
                    .eq(Image::getImageName, "admin_avatar")
                    .set(StrUtil.isNotBlank(userAvatarUrl), Image::getImageUrl, userAvatarUrl);
            update = imageMapper.update(null, updateImgWrapper);
        }

        //3.更新管理员信息
        LambdaUpdateWrapper<Admin> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Admin::getAdminId, adminId)
                .set(StrUtil.isNotBlank(adminUsername), Admin::getAdminUsername, adminUsername)
                .set(StrUtil.isNotBlank(adminPhone), Admin::getAdminPhone, adminPhone);
        return (adminMapper.update(null, updateWrapper) + update) > 0;

    }

    @Override
    public AdminDateVO queryAdminData() {
        AdminDateVO adminDateVO = new AdminDateVO();
        QueryWrapper<Job> jobQueryWrapper = new QueryWrapper<>();
        jobQueryWrapper.eq("job_status", "正常招聘")
                .eq("job_admin_status", "正常");
        adminDateVO.setJobCount(jobMapper.selectCount(jobQueryWrapper));
        QueryWrapper<Employer> employerQueryWrapper = new QueryWrapper<>();
        employerQueryWrapper.eq("employer_status", "正常");
        adminDateVO.setEmployerCount(employerMapper.selectCount(employerQueryWrapper));
        QueryWrapper<Jobseeker> jobseekerQueryWrapper = new QueryWrapper<>();
        jobseekerQueryWrapper.eq("jobseeker_status", "正常");
        adminDateVO.setUserCount(jobseekerMapper.selectCount(jobseekerQueryWrapper));
        return adminDateVO;

    }

    @Override
    public AdminEmployerDataVO queryAdminEmployerData() {
        AdminEmployerDataVO adminEmployerDataVO = new AdminEmployerDataVO();
        QueryWrapper<Employer> employerQueryWrapper = new QueryWrapper<>();
        employerQueryWrapper.eq("employer_status", "正常");
        employerMapper.selectList(employerQueryWrapper).forEach(employer -> {
            switch (employer.getEmployerCompanyIndustry()) {
                case "IT/互联网":
                    adminEmployerDataVO.setInternetEmployerCount(adminEmployerDataVO.getInternetEmployerCount() + 1);
                    break;
                case "服务/餐饮":
                    adminEmployerDataVO.setServiceEmployerCount(adminEmployerDataVO.getServiceEmployerCount() + 1);
                    break;
                case "教育/培训":
                    adminEmployerDataVO.setEducationEmployerCount(adminEmployerDataVO.getEducationEmployerCount() + 1);
                    break;
                case "金融/制造":
                    adminEmployerDataVO.setFinanceEmployerCount(adminEmployerDataVO.getFinanceEmployerCount() + 1);
                    break;
                case "游戏/电子":
                    adminEmployerDataVO.setGameEmployerCount(adminEmployerDataVO.getGameEmployerCount() + 1);
                    break;
                case "网络社交/媒体":
                    adminEmployerDataVO.setNetworkEmployerCount(adminEmployerDataVO.getNetworkEmployerCount() + 1);
                    break;
                case "零售/批发":
                    adminEmployerDataVO.setRetailEmployerCount(adminEmployerDataVO.getRetailEmployerCount() + 1);
                    break;
                case "其他行业":
                    adminEmployerDataVO.setOtherEmployerCount(adminEmployerDataVO.getOtherEmployerCount() + 1);
                    break;
            }
        });
        return adminEmployerDataVO;
    }

    @Override
    public AdminEducationDataVO queryAdminEducationData() {
        AdminEducationDataVO adminEducationDataVO = new AdminEducationDataVO();
        QueryWrapper<Jobseeker> jobseekerQueryWrapper = new QueryWrapper<>();
        jobseekerQueryWrapper.eq("jobseeker_status", "正常");
        jobseekerMapper.selectList(jobseekerQueryWrapper).forEach(jobseeker -> {
            switch (jobseeker.getJobseekerEducationLevel()) {
                case "大专":
                    adminEducationDataVO.setCollegeCount(adminEducationDataVO.getCollegeCount() + 1);
                    break;
                case "本科":
                    adminEducationDataVO.setUndergraduateCount(adminEducationDataVO.getUndergraduateCount() + 1);
                    break;
                case "硕士":
                    adminEducationDataVO.setMasterCount(adminEducationDataVO.getMasterCount() + 1);
                    break;
                case "博士":
                    adminEducationDataVO.setDoctorCount(adminEducationDataVO.getDoctorCount() + 1);
                    break;
                case "其他":
                    adminEducationDataVO.setOtherCount(adminEducationDataVO.getOtherCount() + 1);
                    break;
            }
        });
        return adminEducationDataVO;
    }

    @Override
    public AdminJobDataVO queryAdminJobData() {
        AdminJobDataVO adminJobDataVO = new AdminJobDataVO();

        LocalDate today = LocalDate.now();
        // 7天前
        LocalDate sevenDaysAgo = today.minusDays(7);
        adminJobDataVO.setSevenDayJobCount(countJobsByDate(convertToDate(sevenDaysAgo)));

        // 6天前
        LocalDate sixDaysAgo = today.minusDays(6);
        adminJobDataVO.setSixDayJobCount(countJobsByDate(convertToDate(sixDaysAgo)));

        // 5天前
        LocalDate fiveDaysAgo = today.minusDays(5);
        adminJobDataVO.setFiveDayJobCount(countJobsByDate(convertToDate(fiveDaysAgo)));

        // 4天前
        LocalDate fourDaysAgo = today.minusDays(4);
        adminJobDataVO.setFourDayJobCount(countJobsByDate(convertToDate(fourDaysAgo)));

        // 3天前
        LocalDate threeDaysAgo = today.minusDays(3);
        adminJobDataVO.setThreeDayJobCount(countJobsByDate(convertToDate(threeDaysAgo)));

        // 2天前
        LocalDate twoDaysAgo = today.minusDays(2);
        adminJobDataVO.setTwoDayJobCount(countJobsByDate(convertToDate(twoDaysAgo)));

        // 1天前
        LocalDate oneDayAgo = today.minusDays(1);
        adminJobDataVO.setOneDayJobCount(countJobsByDate(convertToDate(oneDayAgo)));

        return adminJobDataVO;
    }

    private int countJobsByDate(Date date) {
        // 调用 JobMapper 中的方法根据创建时间统计岗位数量
        QueryWrapper<Job> jobQueryWrapper = new QueryWrapper<>();
        jobQueryWrapper.eq("create_time", date);
        return Math.toIntExact(jobMapper.selectCount(jobQueryWrapper));
    }

    private Date convertToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }
}




